import { Inject, Post, Get, Body, Query } from '@midwayjs/core';
import { CoolController, BaseController, CoolTag, TagTypes } from '@cool-midway/core';
import { AutomationPipelineEntity } from '../../entity/pipeline'; 
import { AutomationConditionEntity } from '../../entity/condition';
import { AutomationTaskEntity } from '../../entity/task';
import { AutomationPipelineService } from '../../service/app/pipeline';

/**
 * App端-自动化流水线
 */
// @CoolTag(TagTypes.IGNORE_TOKEN)
@CoolController({
  api: ['page', 'list'],
  entity: AutomationPipelineEntity,
  service: AutomationPipelineService,
  pageQueryOp: {
    keyWordLikeFields: ['a.name'],
    fieldEq: ['a.status', 'a.conditionRelation'],
    where: async (ctx) => {
      const { createTime, triggerType, effectDateType } = ctx.request.body;
      const conditions = [];
      if (createTime && createTime.length === 2) {
        conditions.push([`a.createTime BETWEEN '${createTime[0]}' AND '${createTime[1]}'`]);
      }
      if (triggerType !== undefined && triggerType !== null) {
        conditions.push([`a.triggerType = ${triggerType}`]);
      }
      if (effectDateType !== undefined && effectDateType !== null) {
        conditions.push([`a.effectDateType = ${effectDateType}`]);
      }
      return conditions;
    },
    select: ['a.*'],
    join: [],
  },
})
export class AppAutomationPipelineController extends BaseController {
  @Inject()
  automationPipelineService: AutomationPipelineService;

  /**
   * 获取流水线详情
   */
  @Get('/detail', { summary: '获取流水线详情' })
  async pipelineDetail(@Query('id') id: number) {
    return this.ok(await this.automationPipelineService.pipelineDetail(id));
  }

  /**
   * 获取我的自动化流水线列表
   */
  // @Get('/myPipelines', { summary: '获取我的自动化流水线列表' })
  // async getMyPipelines() {
  //   const pipelines = await this.automationPipelineService.list();
  //   return this.ok(pipelines);
  // }

  // /**
  //  * 获取启用的流水线列表
  //  */
  // @Get('/enabledPipelines', { summary: '获取启用的流水线列表' })
  // async getEnabledPipelines() {
  //   const pipelines = await this.automationPipelineService.list({ status: 1 });
  //   return this.ok(pipelines);
  // }

  /**
   * 获取条件类型选项
   */
  @Get('/conditionTypes', { summary: '获取条件类型选项' })
  async getConditionTypes() {
    return this.ok(await this.automationPipelineService.getConditionTypes());
  }

  /**
   * 获取气象条件类型选项
   */
  @Get('/weatherTypes', { summary: '获取气象条件类型选项' })
  async getWeatherTypes() {
    return this.ok(await this.automationPipelineService.getWeatherTypes());
  }

  /**
   * 获取比较方式选项
   */
  @Get('/compareTypes', { summary: '获取比较方式选项' })
  async getCompareTypes() {
    return this.ok(await this.automationPipelineService.getCompareTypes());
  }

  /**
   * 获取任务操作类型选项
   */
  @Get('/actionTypes', { summary: '获取任务操作类型选项' })
  async getActionTypes() {
    return this.ok(await this.automationPipelineService.getActionTypes());
  }

  /**
   * 获取流水线运行状态
   */
  // @Get('/runningStatus', { summary: '获取流水线运行状态' })
  // async getRunningStatus() {
  //   const total = await this.automationPipelineService.count();
  //   const enabled = await this.automationPipelineService.count({ status: 1 });
    
  //   return this.ok({
  //     total,
  //     enabled,
  //     disabled: total - enabled
  //   });
  // }
}